<?php
/**
  * Musashi - A project manager for music groups and bands
  * Copyright (C) 2010 Daniel Torres
  *
  * This program is free software: you can redistribute it and/or modify
  * it under the terms of the GNU General Public License as published by
  * the Free Software Foundation, either version 3 of the License, or
  * (at your option) any later version.
  *
  * This program is distributed in the hope that it will be useful,
  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  * GNU General Public License for more details.
  *
  * You should have received a copy of the GNU General Public License
  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
  *
  */

/**
 *  Controls actions related to the costumes module of musashi
 *  
 * @package com_musashi
 * @author Daniel Torres
 */
 // Make sure we have access to this page
defined('_JEXEC') or die ('Restricted access');

// Controller
jimport('joomla.application.component.controller');

// Make sure this user is a registered one. Otherwise, send him to the registration page
require_once(JPATH_COMPONENT.DS.'models'.DS.'user.php');

class MusashiControllerCostumes extends JController
{
    
    /**
     *  Makes sure this operation is valid by checking the validFormCheck flag.
     *  The flag is automatically reset upon call.
     */
    function isValidForm()
    {
        $session =& JFactory::getSession();
        $result = $session->get('validFormCheck',false);
        $session->set('validFormCheck',false);
        
        return $result;
    }
    
    /**
     *  Adds selected pieces of clothing to the user's inventory
     */
    function addClothing()
    {
        $model   =& $this->getModel('costumes');
        $session =& JFactory::getSession();
        $cloth   =  JRequest::getInt('itemid');
        
        // Use the model to add all those pieces of clothing.
        $clothArray[] = $cloth;
        if( $model->addClothingArray($clothArray) == false )
        {
            $session->set('actionResult','Failed to update clothing!');
        }        
        
        
        $route = JRoute::_('index.php?option=com_musashi&controller=costumes&view=costumes',false);
        $this->setRedirect($route);

    }
    
    /**
     *  Adds the components of a given costume into the user's inventory
     */
    function addCostume()
    {
        $model   =& $this->getModel('costumes');
        $session =& JFactory::getSession();
        $costume =  JRequest::getInt('itemid');
        
        if( $model->addCostume($costume))
        {
            $session->set('actionResult','Information updated!');            
        }
        else
        {
            $session->set('actionResult','Failed to update clothing!');
        }        
        
        
        $route = JRoute::_('index.php?option=com_musashi&controller=costumes&view=costumes',false);
        $this->setRedirect($route);
        
    }
    
    /**
     *  Removes selected pieces of clothing from the user's inventory
     */
    function removeClothing()
    {
        $model   =& $this->getModel('costumes');
        $session =& JFactory::getSession();        
        
        // Use the model to remove all those pieces of clothing
        $selectedPieces[] = JRequest::getInt('itemid');
        if( $model->removeClothingArray($selectedPieces) == false)
        {
            $session->set('actionResult','Failed to update clothing!');
        }        
        
        
        $route = JRoute::_('index.php?option=com_musashi&controller=costumes&view=costumes',false);
        $this->setRedirect($route);
        
    }
    
  
    /**
     * Displays the main Costumes view
     * @access public
     */
    function display()
    {
        // Make sure we're registered  users
        if( MusashiModelUser::validateUser( 'http://'.$_SERVER["SERVER_NAME"].$_SERVER["REQUEST_URI"] ) == false )
        {
            // Redirect to registration view
            $url = MusashiModelUser::getUnregisteredUserPath();
            $this->setRedirect( $url );            
        }

        parent::display();
    }       
}

?>